<template>
  <el-dialog
    title="绑定小程序用户"
    v-model="bandOpen"
    width="400px"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-form ref="bandFormRef" :model="bandForm" label-width="80px">
      <el-form-item label="用户名" prop="userId" v-if="mobileDisabled">
        <el-select
          v-model="bandForm.userId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入手机号"
          :remote-method="remoteMethod"
          :loading="loading"
        >
          <el-option v-for="item in options" :key="item.id" :label="item.mobile" :value="item.id">
            <span style="float: left">{{ item.nickName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.mobile }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="场馆" prop="stadiumId">
        <el-select
          v-model="bandForm.stadiumId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入场馆名"
          :remote-method="handleQueryStadium"
          :loading="loading"
        >
          <el-option
            v-for="item in stadiumOptions"
            :key="item.id"
            :label="item.stadiumName"
            :value="item.id"
          >
            <span style="float: left">{{ item.stadiumName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="bandOpen = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { bindUserV2, getMiniUser } from '@/api/system/appUser'
import { getStadiumInfo } from '@/api/stadium/stadium'
import { ElMessage, ElForm } from 'element-plus'

// 定义组件属性和事件
const emit = defineEmits(['refreshShow'])

// 响应式数据
const bandOpen = ref(false)
const bandFormRef = ref<InstanceType<typeof ElForm>>()
const bandForm = reactive({
  sysUserId: undefined as number | undefined,
  userId: undefined as number | undefined,
  stadiumId: undefined as number | undefined,
  deptId: undefined as number | undefined,
})

const options = ref<any[]>([])
const stadiumOptions = ref<any[]>([])
const loading = ref(false)
const userData = ref<any>({})
const mobileDisabled = ref(true)

// 初始化方法
const init = (data: any) => {
  // 重置表单
  Object.assign(bandForm, {
    sysUserId: undefined,
    userId: undefined,
    stadiumId: undefined,
    deptId: undefined,
  })

  options.value = []
  stadiumOptions.value = []
  userData.value = data
  bandForm.sysUserId = data.userId
  bandForm.deptId = data.deptId

  if (data.bandStadiums && data.bandStadiums != null && data.bandStadiums.length > 0) {
    mobileDisabled.value = false
    bandForm.userId = data.bandStadiums[0].userId
  } else {
    mobileDisabled.value = true
  }
  bandOpen.value = true
}

// 查询用户
const remoteMethod = (query: string) => {
  if (query !== '') {
    loading.value = true
    getMiniUser({ mobile: query })
      .then((res) => {
        options.value = res.data
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  } else {
    options.value = []
  }
}

// 查询场馆
const handleQueryStadium = (query: string) => {
  if (query !== '') {
    loading.value = true
    getStadiumInfo({ stadiumName: query })
      .then((res) => {
        stadiumOptions.value = res.data
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  } else {
    stadiumOptions.value = []
  }
}

// 绑定用户
const submitForm = () => {
  if (bandForm.userId != null) {
    loading.value = true
    bindUserV2(bandForm)
      .then((res) => {
        loading.value = false
        ElMessage.success('绑定成功')
        bandOpen.value = false
        emit('refreshShow')
      })
      .catch((e) => {
        loading.value = false
      })
  } else {
    ElMessage.error('请选择用户')
  }
}

// 暴露方法给父组件
defineExpose({
  init,
})
</script>

<style scoped lang="scss">
.dialog-footer {
  text-align: right;
}
</style>
